﻿using IRepository.SystemDefault;
using Newtonsoft.Json;
using ServiceStack.Redis;
using System;
using System.Text;
using System.Collections.Generic;
using Common.Data;

namespace Repository.SystemDefault
{
    public class DataQueueRepository : IDataQueueRepository
    {
        private static Dictionary<string, IRedisClient> dicRedisClients = null;
        private static object objLock = null;
        static DataQueueRepository()
        {
            dicRedisClients = new Dictionary<string, IRedisClient>();
            objLock = new object();
        }
        private static IRedisClient GetRedisClient(string constr)
        {
            if (!dicRedisClients.ContainsKey(constr))
            {
                lock (objLock)
                {
                    if (!dicRedisClients.ContainsKey(constr))
                    {
                        using (var redisManagerPool = new RedisManagerPool(constr))
                        {
                            var client = redisManagerPool.GetClient();
                            dicRedisClients.Add(constr, client);
                            return client;
                        }
                    }
                }
            }
            return dicRedisClients[constr];
            //using (var redisManagerPool = new RedisManagerPool(constr))
            //{
            //    var client = redisManagerPool.GetClient();
            //    return client;
            //}
        }


        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="constr"></param>
        /// <param name="key"></param>
        /// <param name="data"></param>
        private void Enqueue(string constr, string key, object data)
        {
            var value = JsonConvert.SerializeObject(data);
            value = DESEncrypt.Encrypt(value, "12345678");

            using (var redisManagerPool = new RedisManagerPool(constr))
            {
                using (var client = redisManagerPool.GetClient())
                {
                    client.EnqueueItemOnList(key, value);
                }
            }
            //try
            //{
            //    var client = GetRedisClient(constr);
           
            //    client.EnqueueItemOnList(key, value);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}
        }
        /// <summary>
        /// 出队
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="constr"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private T Dequeue<T>(string constr, string key)
        {
            using (var redisManagerPool = new RedisManagerPool(constr))
            {
                using (var client = redisManagerPool.GetClient())
                {
                    try
                    {
                        var value = client.DequeueItemFromList(key);
                        if (value == null) return default(T);
                        value = DESEncrypt.Decrypt(value, "12345678");
                        return JsonConvert.DeserializeObject<T>(value);
                    }
                    catch (Exception)
                    {
                        return default(T);
                    };
                }
            }

            //var client = GetRedisClient(constr);
            //try
            //{
            //    var value = client.DequeueItemFromList(key);
            //    if(value==null) return default(T);
            //    value = DESEncrypt.Decrypt(value,"12345678");
            //    return JsonConvert.DeserializeObject<T>(value);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //    return default(T);
            //};
        }
        #region 待处理的队列
        public void AddCollectCollectUrlInfo(int configID, object data, string constr)
        {
            Enqueue(constr, "WaitToCollectUrl-"+configID, data);
        }


        public T GetCollectUrlInfo<T>(int configID, string constr)
        {
            return Dequeue<T>(constr, "WaitToCollectUrl-" + configID);
        }
        #endregion

        #region 待排重的原始数据
        public void AddOriginalCollectUrlInfo(int configID, object data, string constr)
        {
            Enqueue(constr, "OriginalCollectUrl-" + configID, data);
        }


        public T GetOriginalUrlInfo<T>(int configID, string constr)
        {
            return Dequeue<T>(constr, "OriginalCollectUrl-" + configID);
        }
        #endregion

        #region 待解析的页面内容
        public void AddAnalyzePageInfo(int configID, object data, string constr)
        {
            Enqueue(constr, "AnalyzePageInfo-" + configID, data);
        }


        public T GetAnalyzePageInfo<T>(int configID,string constr)
        {
            return Dequeue<T>(constr, "AnalyzePageInfo-" + configID);
        }

        
        #endregion
    }
}
